﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.LLBLGen.Pro.ORMSupportClasses;
using MyHealthOnline.Dal.Generated.EntityClasses;
using MyHealthOnline.Dal.Generated.CollectionClasses;
using MyHealthOnline.Dal.Generated.HelperClasses;

namespace MyHealthOnline.Business
{
    public class QuestionnaireBusiness
    {
        public static QuestionCollection GetQuestionnaires()
        {
            PredicateExpression filter = new PredicateExpression(QuestionFields.Type == "Questionnaire");
            RelationCollection relColl = new RelationCollection();
            relColl.Add(QuestionEntity.Relations.QuestionnaireEntityUsingQuestionId);
            QuestionCollection questionColl = new QuestionCollection();
            questionColl.GetMulti(filter, 0, null, relColl);

            return questionColl;
        }

        public static QuestionCollection GetQuestionnairesByConsult(int consultId)
        {
            PredicateExpression consultFilter = new PredicateExpression(ConsultQuestionFields.ConsultId == consultId);
            RelationCollection relColl = new RelationCollection();
            relColl.Add(QuestionEntity.Relations.ConsultQuestionEntityUsingQuestionId).CustomFilter = consultFilter;
            QuestionCollection questionColl = GetQuestionnaires();
            questionColl.GetMulti(null, 0, null, relColl);

            return questionColl;
        }

        public static QuestionCollection GetUnansweredQuestionnairesByConsult(int consultId)
        {
            PredicateExpression filter = new PredicateExpression();
            filter.Add(new FieldCompareNullPredicate(QuestionFields.AnswerDate));
            QuestionCollection questionColl = GetQuestionnairesByConsult(consultId);
            questionColl.GetMulti(filter);
            return questionColl;
        }
        
        public static void SendQuestionnaire(int questionId, int consultId)
        {
            PredicateExpression filter=new PredicateExpression(QuestionFields.Id == questionId);
            QuestionCollection questionColl = new QuestionCollection();
            questionColl.GetMulti(filter);

            if (questionColl.Count()>0)
            {
                QuestionEntity question = questionColl.ElementAt(0);
                CreateQuestionnaire(question);
                QuestionBusiness.AddQuestionToConsult(question, consultId);
            }
        }

        public static int CreateQuestionnaire(string title)
        {
            QuestionEntity newQuestion = QuestionBusiness.CreateNewQuestion("Questionnaire",title, false);
            
            QuestionnaireEntity newQuestionnaire = new QuestionnaireEntity();
            newQuestionnaire.QuestionId = newQuestion.Id;
            newQuestionnaire.Question = newQuestion;
            newQuestion.Questionnaire = newQuestionnaire;
            newQuestionnaire.Save();

            return newQuestionnaire.Id;
        }
       
        private static void CreateQuestionnaire(QuestionEntity question)
        {
            QuestionEntity newQuestion = QuestionBusiness.CreateNewQuestion(question);

            QuestionnaireEntity newQuestionnaire = new QuestionnaireEntity();
            newQuestionnaire.QuestionId = newQuestion.Id;
            newQuestionnaire.Question = newQuestion;
            newQuestion.Questionnaire = newQuestionnaire;
            newQuestionnaire.Save();

            foreach (QuestionnaireQuestionEntity questionnaireQuestion in question.Questionnaire.QuestionnaireQuestion.Items)
            {
                QuestionEntity oldQuestionInOldQuestionnaireQuestion = questionnaireQuestion.Question;
                QuestionEntity newQuestionInQuestionnaireQuestion =
                    QuestionBusiness.CreateNewQuestionAndRelatedQuestionType(oldQuestionInOldQuestionnaireQuestion);

                CreateNewQuestionnaireQuestion(newQuestionnaire, newQuestionInQuestionnaireQuestion);
            }

            newQuestionnaire.Save();
        }

        private static void CreateNewQuestionnaireQuestion(QuestionnaireEntity newQuestionnaire, QuestionEntity newQuestionInQuestionnaireQuestion)
        {
            QuestionnaireQuestionEntity newQuestionnaireQuestion = new QuestionnaireQuestionEntity();
            newQuestionnaireQuestion.QuestionnaireId = newQuestionnaire.Id;
            newQuestionnaireQuestion.QuestionId = newQuestionInQuestionnaireQuestion.Id;
            newQuestionnaireQuestion.Question = newQuestionInQuestionnaireQuestion;
            newQuestionnaireQuestion.Questionnaire = newQuestionnaire;
            newQuestionnaireQuestion.Save();
            newQuestionInQuestionnaireQuestion.Save();
            newQuestionnaire.QuestionnaireQuestion.Add(newQuestionnaireQuestion);
            newQuestionnaire.Save();
        }


        public static QuestionnaireEntity GetQuestionnaire(int questionnaireId)
        {
            PredicateExpression filter = new PredicateExpression(QuestionnaireFields.Id == questionnaireId);
            QuestionnaireCollection questionnaireColl = new QuestionnaireCollection();
            questionnaireColl.GetMulti(filter);
            if (questionnaireColl.Count>0)
                return questionnaireColl.ElementAt(0);

            return null;
        }

        public static void AddOpenQuestion(int questionnaireId, string questionText, bool include)
        {
            QuestionEntity question = QuestionBusiness.CreateNewQuestion("Open", questionText, include);
            QuestionBusiness.CreateOpenQuestion(question);
            question.Save();

            AddQuestionToQuestionnaire(question, questionnaireId);
        }

        public static bool AddQuestionToQuestionnaire(QuestionEntity question, int questionnaireId)
        {
            try
            {
                PredicateExpression filter = new PredicateExpression(QuestionnaireFields.Id == questionnaireId);
                QuestionnaireCollection questionnaireColl = new QuestionnaireCollection();
                questionnaireColl.GetMulti(filter);
                if (questionnaireColl.Count() > 0)
                {
                    QuestionnaireEntity questionnaire = questionnaireColl.ElementAt(0);
                    CreateNewQuestionnaireQuestion(questionnaire, question);
                }
                return true;

            }
            catch
            {
                return false;
            }
        }

        public static void AddMultiQuestion(int questionnaireId, string questionText, string choices, bool include)
        {
            QuestionEntity question = QuestionBusiness.CreateNewQuestion("Multi", questionText, include);
            QuestionBusiness.CreateMultiQuestion(choices, question);
            question.Save();

            AddQuestionToQuestionnaire(question, questionnaireId);
        }

        public static void AddDateQuestion(int questionnaireId, string questionText, bool include)
        {
            QuestionEntity question = QuestionBusiness.CreateNewQuestion("Date", questionText, include);
            QuestionBusiness.CreateDateQuestion(question);
            question.Save();

            AddQuestionToQuestionnaire(question, questionnaireId);
        }

        public static void AddDatabaseQuestion(int questionnaireId, int dbQuestionId)
        {
            PredicateExpression filter = new PredicateExpression(QuestionFields.Id == dbQuestionId);
            QuestionCollection questionColl = new QuestionCollection();
            questionColl.GetMulti(filter);

            if (questionColl.Count() > 0)
            {
                QuestionEntity question = new QuestionEntity();
                question = questionColl.ElementAt(0);

                QuestionEntity newQuestion = QuestionBusiness.CreateNewQuestionAndRelatedQuestionType(question);

                AddQuestionToQuestionnaire(newQuestion, questionnaireId);
            }
        }

        public static void DeleteQuestion(int id)
        {
            PredicateExpression filter = new PredicateExpression(QuestionFields.Id == id);
            QuestionCollection questionColl = new QuestionCollection();
            questionColl.DeleteMulti(filter);
        }
    }
}
